using System;
using System.Collections.Generic;
using System.Text;

namespace Possan.Graphics.Filters
{
	/// <summary>
	/// Keeps track of all filters
	/// </summary>
	public class FilterLibrary
	{
		private static Dictionary<string, Type> alltypes = null;

		private static void _RegisterBuiltin()
		{
			// add built in filters and their aliases here
			Register("addborder", typeof(AddBorder));
			Register("addpadding", typeof(AddPadding));
			Register("blur", typeof(BlurFilter));
			Register("contrast", typeof(ContrastFilter));
			Register("crop", typeof(Crop));
			Register("dropshadow", typeof(DropShadow));
			Register("fillbackground", typeof(FillBackground));
			Register("fillcolor", typeof(FillColor));
			Register("glow", typeof(GlowFilter));
			Register("grayscale", typeof(Grayscale));
			Register("oldresize", typeof(OldResizeFilter));
			Register("overlay", typeof(OverlayFilter));
			Register("resize", typeof(ResizeFilter));
			Register("rotate", typeof(Rotate));
			Register("roundcorners", typeof(RoundCorners));
			Register("setopacity", typeof(SetOpacity));
			Register("skinbackground", typeof(SkinBackground));
			Register("skinoverlay", typeof(SkinOverlay));
			Register("trim", typeof(Trimmer));
		}

		private static void _EnsureList()
		{
			if (alltypes == null)
			{
				alltypes = new Dictionary<string, Type>();
				_RegisterBuiltin();
			}
		}

		/// <summary>
		/// Registers a filter by instance
		/// </summary>
		/// <param name="name">Filter name</param>
		/// <param name="filter">Filter instance</param>
		public static void Register(string name, IBaseFilter filter)
		{
			Register(name, filter.GetType());
		}

		/// <summary>
		/// Registers a filter by type
		/// </summary>
		/// <param name="name">Filter name</param>
		/// <param name="filtertype">Filter type</param>
		public static void Register(string name, Type filtertype)
		{
			_EnsureList();
			if (alltypes.ContainsKey(name))
				alltypes[name] = filtertype;
			else
				alltypes.Add(name, filtertype);
		}

		/// <summary>
		/// Returns a list of all registered filter names
		/// </summary>
		/// <returns>List of names</returns>
		public static List<string> GetFilterNames()
		{
			_EnsureList();
			List<string> ret = new List<string>();

			Dictionary<string, Type>.Enumerator enu = alltypes.GetEnumerator();
			while (enu.MoveNext())
			{
				ret.Add(enu.Current.Key);
			}
			     return ret;
		}

		/// <summary>
		/// Returns the type for a filter
		/// </summary>
		/// <param name="name">Filter name</param>
		/// <returns>Filter type</returns>
		public static Type GetFilterType(string name)
		{
			_EnsureList();
			if (alltypes.ContainsKey(name))
				return alltypes[name];
			     return null;
		}

		/// <summary>
		/// Returns a named filter
		/// </summary>
		/// <param name="name">Filter name</param>
		/// <returns>The filter</returns>
		public static IBaseFilter CreateFilter(string name)
		{
			IBaseFilter ret = null;
			_EnsureList();

			Type t = null;
			if (alltypes.ContainsKey(name))
				t = alltypes[name];
			if (t != null)
			{
				ret = Activator.CreateInstance(t) as IBaseFilter;
			}

			     return ret;
		}
	}

}





